home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / telecom / 133 / c / tohex.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-04-17  |  9.7 KB  |  190 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /* Module:     tohex.c - Translate any file to hex values                   */
  4. /*                                                                          */
  5. /* Programmer: George R. Woodside                                           */
  6. /*                                                                          */
  7. /* Date:       December 7, 1986 (No bombs, no Japanese attack!)             */
  8. /*                                                                          */
  9. /****************************************************************************/
  10.  
  11. #include <stdio.h>
  12.  
  13. /****************************************************************************/
  14. /*                                                                          */
  15. /* Output mode control flag, set by entering an option on the               */
  16. /* command line (-b, -w, or -l):                                            */
  17. /*                                                                          */
  18. /*    0 = bytes                                                             */
  19. /*    1 = words                                                             */
  20. /*    2 = longs                                                             */
  21. /*                                                                          */
  22. /****************************************************************************/
  23.  
  24. int  mode = 1;                          /* default to words                 */
  25.  
  26. main(argc,argv)
  27. int     argc;
  28. char    *argv[];
  29.  
  30. {
  31.  
  32.   int i;
  33.   FILE  *fp;
  34.  
  35.   if (argc > 1)                         /* if multiple arguments,           */
  36.     {
  37.       for (i = 1; i < argc; i++)        /* then do each one                 */
  38.         {
  39.           if(strcmp(argv[i],"-b") == 0) /* if byte flag,                    */
  40.             mode = 0;                   /* set byte dump mode               */
  41.           else if(strcmp(argv[i],"-w") == 0) /* if word flag,               */
  42.             mode = 1;                   /* set word dump mode               */
  43.           else if(strcmp(argv[i],"-l") == 0) /* if long flag,               */
  44.             mode = 2;                   /* set long dump mode               */
  45.           else if( (fp = fopenb(argv[i],"r") ) == NULL) /* if open error,   */
  46.             {
  47.               printf("%s: Unable to open %s \n",argv[0],argv[i]);
  48.               printf("\n\n");
  49.               continue;
  50.             }                           /* end file open error              */
  51.           else                          /* if file opened OK,               */
  52.             {
  53.               tohex(fp);                /* print it                         */
  54.             }                           /* end file opened OK               */
  55.         }                               /* end for one argument             */
  56.     }                                   /* end for all arguments            */
  57.     else                                /* if no arguments,                 */
  58.       tohex(stdin);                     /* dump standard in                 */
  59. }                                       /* end main                         */
  60.  
  61. /****************************************************************************/
  62. /*                                                                          */
  63. /* See which format to dump in                                              */
  64. /*                                                                          */
  65. /****************************************************************************/
  66.  
  67. tohex(fp)
  68. FILE *fp;                               /* file to dump                     */
  69. {
  70.   if(mode == 0)
  71.     bdump(fp);                          /* byte dump                        */
  72.  
  73.   if(mode == 1)
  74.     wdump(fp);                          /* word dump                        */
  75.  
  76.   if(mode == 2)
  77.     ldump(fp);                          /* long dump                        */
  78. }                                       /* end tohex                        */
  79.  
  80. /****************************************************************************/
  81. /*                                                                          */
  82. /* Byte dump                                                                */
  83. /*                                                                          */
  84. /****************************************************************************/
  85.  
  86. bdump(fp)
  87. FILE *fp;                               /* file to dump                     */
  88.  
  89. {
  90.   register  int    i=0;                 /* set a counter                    */
  91.   register  int    ch;                  /* byte or word dump                */
  92.  
  93.   printf("  ");                         /* set the lead-in                  */
  94.   while(( ch=fgetc(fp)) != EOF)         /* for each character,              */
  95.     {
  96.       printf("0x%02x, ",(int) (ch & 0xff) ); /* print the byte              */
  97.       if(i++ == 12)
  98.         {
  99.           i = 0;                        /* reset the line count             */
  100.           printf("\n  ");               /* move down a line                 */
  101.         }
  102.     }
  103.   if (i != 0)                           /* if any left over bytes,          */
  104.     printf("\n");                       /* a final newline                  */
  105.  
  106.   fclose(fp);                           /* close input file                 */
  107. }
  108.  
  109. /****************************************************************************/
  110. /*                                                                          */
  111. /* Word dump                                                                */
  112. /*                                                                          */
  113. /****************************************************************************/
  114.  
  115. wdump(fp)
  116. FILE *fp;                               /* file to dump                     */
  117.  
  118. {
  119.   register  int    i   = 0;             /* set a counter                    */
  120.   register  int    ch;                  /* byte or word dump                */
  121.   register  int    wch = 0;             /* word dump                        */
  122.  
  123.   printf("  ");                         /* set the lead-in                  */
  124.   while(( ch=fgetc(fp)) != EOF)         /* for each character,              */
  125.     {
  126.       wch = (wch << 8) + ch;            /* add in the new byte              */
  127.       if((++i & 1) == 0)                /* if we have a word,               */
  128.         {
  129.           printf("0x%04x, ",wch );      /* print the word                   */
  130.           wch = 0;                      /* and clear the value              */
  131.         }
  132.       if(i == 16)                       /* if at end of a line,             */
  133.         {
  134.           i = 0;                        /* reset the line count             */
  135.           printf("\n  ");               /* move down a line                 */
  136.         }
  137.     }                                   /* end of file                      */
  138.   if(i & 1)                             /* if an odd byte left over,        */
  139.     printf("0x%04x ",wch << 8 );        /* print the byte                   */
  140.  
  141.   if (i != 0)                           /* if any left over bytes,          */
  142.     printf("\n");                       /* a final newline                  */
  143.  
  144.   fclose(fp);                           /* close the file                   */
  145. }
  146.  
  147. /****************************************************************************/
  148. /*                                                                          */
  149. /* Long dump                                                                */
  150. /*                                                                          */
  151. /****************************************************************************/
  152.  
  153. ldump(fp)
  154. FILE *fp;                               /* file to dump                     */
  155.  
  156. {
  157.   register  int    i   = 0;             /* set a counter                    */
  158.   register  int    ch;                  /* byte or word dump                */
  159.   register  long   lch = 0L;            /* long dump                        */
  160.  
  161.   printf("  ");                         /* set the lead-in                  */
  162.   while(( ch=fgetc(fp)) != EOF)         /* for each character,              */
  163.     {
  164.       lch = (lch << 8) + ch;            /* add in the new byte              */
  165.       if((++i & 3) == 0)                /* if we have a long word,          */
  166.         {
  167.           printf("0x%08X, ",lch );      /* print the long                   */
  168.           lch = 0L;                     /* and clear the value              */
  169.         }
  170.       if(i == 16)                       /* if at end of a line,             */
  171.         {
  172.           i = 0;                        /* reset the line count             */
  173.           printf("\n  ");               /* move down a line                 */
  174.         }
  175.     }                                   /* end of file                      */
  176.   if((i & 3) == 1)                      /* if one odd byte left over,       */
  177.     printf("0x%08X ",lch << 24 );       /* print the byte                   */
  178.  
  179.   if((i & 3) == 2)                      /* if two odd bytes left over,      */
  180.     printf("0x%08X ",lch << 16 );       /* print them                       */
  181.  
  182.   if((i & 3) == 3)                      /* if three odd bytes left over,    */
  183.     printf("0x%08X ",lch << 8 );        /* print them                       */
  184.  
  185.   if (i != 0)                           /* if any left over bytes,          */
  186.     printf("\n");                       /* a final newline                  */
  187.  
  188.   fclose(fp);                           /* close the file                   */
  189. }
  190.